from django.db.models import Q, Sum
from rest_framework import serializers

from databases.models import *
from databases.serializers import *


class AdminUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = AdminUser
        fields = (
            'id', 'account',
        )


class PostAdminLoginSerializer(serializers.ModelSerializer):
    phone = serializers.CharField(label="手机号码", write_only=True)
    password = serializers.CharField(label="密码", write_only=True)

    class Meta:
        model = AdminUser
        fields = (
            'password', 'phone',
        )


class PostAdminChangePasswordSerializer(serializers.ModelSerializer):
    old = serializers.CharField(label="老密码", write_only=True)
    password = serializers.CharField(label="新密码", write_only=True)

    class Meta:
        model = AdminUser
        fields = (
            'password', 'old',
        )


class PostAdminForgetPasswordSerializer(serializers.ModelSerializer):
    code = serializers.CharField(label="短信验证码", write_only=True)
    password = serializers.CharField(label="新密码", write_only=True)

    class Meta:
        model = AdminUser
        fields = (
            'password', 'code'
        )


class PostSaveInfoSerializer(serializers.ModelSerializer):
    background = serializers.CharField(label="背景图", write_only=True)
    cover = serializers.CharField(label="封面图", write_only=True)
    text = serializers.CharField(label="资讯正文", write_only=True)
    title = serializers.CharField(label="资讯标题", write_only=True)

    class Meta:
        model = Information
        fields = (
            'background', 'cover', 'text', 'title'
        )


class AdminAffairsRecordSerializer(serializers.ModelSerializer):
    user_info = serializers.SerializerMethodField()
    comment_num = serializers.SerializerMethodField()
    class Meta:
        model = AffairsRecord
        fields = (
            'id', 'user_id', 'create_time', 'affair_text', 'affair_photo', 'browse_num', 'begin_weight',
            'present_weight', 'target_weight', 'user_info', 'comment_num', 'is_recommend', 'base_thumbs_num','thumbs_num'
        )

    def get_user_info(self, obj):
        script = UserInfo.objects.get(pk=obj.user_id)
        return UserInfoSerializer(script).data

    def get_comment_num(self, obj):
        script = CommentRecord.objects.filter(affair_id=obj.id)
        return len(script)


class PostSaveTrainSerializer(serializers.ModelSerializer):
    background = serializers.CharField(label="背景图", write_only=True)
    cover = serializers.CharField(label="背景图", write_only=True)
    introduce = serializers.CharField(label="文字介绍", write_only=True)
    name = serializers.CharField(label="训练名字", write_only=True)
    video = serializers.CharField(label="训练视频", write_only=True)
    is_warm = serializers.BooleanField(label="是否热身", write_only=True)
    base = serializers.IntegerField(label="基础加入量", write_only=True)

    class Meta:
        model = Training
        fields = (
            'background', 'name', 'video', 'introduce', 'cover', 'is_warm', 'base',
        )


class PostSearchUserSerializer(serializers.ModelSerializer):
    search_text = serializers.CharField(label="搜索文本", write_only=True)

    class Meta:
        model = UserInfo
        fields = (
            'search_text',
        )


class AdminUserInfoSerializer(serializers.ModelSerializer):
    connect_num = serializers.SerializerMethodField()
    affair_num = serializers.SerializerMethodField()
    connect_type = serializers.SerializerMethodField()
    user_medal = serializers.SerializerMethodField()

    class Meta:
        model = UserInfo
        fields = (
            'id',
            'weight',
            'height',
            'nick_name',
            'sex',
            'city',
            'age',
            'portrait',
            'birth_year', 'birth_month', 'is_notify', 'balance', 'total_sport_time', 'continuity_sign', 'exp',
            'affair_num',
            'connect_num',
            'create_time',
            'target_weight',
            'connect_type',
            'medals',
            'user_medal','idcard_no','is_vip','is_certification','idcard_img_bg','idcard_img_fg'
        )

    def get_affair_num(self, obj):
        script = AffairsRecord.objects.filter(user_id=obj.id)
        return len(script)

    def get_connect_num(self, obj):
        script = ConnectionRecord.objects.filter(user_id=obj.id)
        return len(script)

    def get_user_medal(self, obj):
        try:
            _medal = obj.medals
            script = UserMedal.objects.filter(pk__in=_medal.split(','))
            return UserMedalSerializer(script, many=True).data
        except:
            return None

    def get_connect_type(self, obj):
        try:
            type1 = ConnectionTypeRecord.objects.filter(duration__isnull=False, user_id=obj.id, type=1,duration__lt=18000).values(
                'duration').annotate(result=Sum('duration'))[0]['result']
            _hour = int(type1 / 3600)
            _minute = int((type1 - 3600 * _hour) / 60)
            _seconds = int((type1 - 3600 * _hour - 60 * _minute) % 60)
            if _hour < 10:
                _hour = '0' + str(_hour)
            if _minute < 10:
                _minute = '0' + str(_minute)
            if _seconds < 10:
                _seconds = '0' + str(_seconds)
            _time1 = str(_hour) + ':' + str(_minute) + ':' + str(_seconds)
        except:
            type1 = 0
            _time1 = "00:00:00"
        try:
            type2 = ConnectionTypeRecord.objects.filter(duration__isnull=False, user_id=obj.id, type=2,duration__lt=18000).values(
                'duration').annotate(result=Sum('duration'))[0]['result']
            _hour = int(type2 / 3600)
            _minute = int((type2 - 3600 * _hour) / 60)
            _seconds = int((type2 - 3600 * _hour - 60 * _minute) % 60)
            if _hour < 10:
                _hour = '0' + str(_hour)
            if _minute < 10:
                _minute = '0' + str(_minute)
            if _seconds < 10:
                _seconds = '0' + str(_seconds)
            _time2 = str(_hour) + ':' + str(_minute) + ':' + str(_seconds)
        except:
            type2 = 0
            _time2 = "00:00:00"
        try:
            type3 = ConnectionTypeRecord.objects.filter(duration__isnull=False, user_id=obj.id, type=3,duration__lt=18000).values(
                'duration').annotate(result=Sum('duration'))[0]['result']
            _hour = int(type3 / 3600)
            _minute = int((type3 - 3600 * _hour) / 60)
            _seconds = int((type3 - 3600 * _hour - 60 * _minute) % 60)
            if _hour < 10:
                _hour = '0' + str(_hour)
            if _minute < 10:
                _minute = '0' + str(_minute)
            if _seconds < 10:
                _seconds = '0' + str(_seconds)
            _time3 = str(_hour) + ':' + str(_minute) + ':' + str(_seconds)
        except:
            type3 = 0
            _time3 = "00:00:00"
        return_dict = {"reduce": _time1, "compact": _time2, "physiotherapy": _time3}
        return return_dict


class AdminUserBodyDataHistorySerializer(serializers.ModelSerializer):
    class Meta:
        model = BodyData
        fields = (
            'used_count', 'fat_percentage_avg_sofar', 'weight_avg_sofar',
        )
